﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentNHibernate.Conventions;

namespace Commander.DataLayer.Conventions {
  public class ClassMappingConvention : IClassConvention {
    public void Apply(FluentNHibernate.Conventions.Instances.IClassInstance instance) {
      instance.Table(PluralOf(instance.EntityType.Name));
    }

    public static string PluralOf(string text) {
      var pluralString = text;
      var lastCharacter = pluralString.Substring(pluralString.Length - 1).ToLower();

      // y’s become ies (such as Category to Categories)
      if (string.Equals(lastCharacter, "y", StringComparison.InvariantCultureIgnoreCase)) {
        pluralString = pluralString.Remove(pluralString.Length - 1);
        pluralString += "ie";
      }

      //// ch’s become ches (such as Pirch to Pirches)
      if (string.Equals(pluralString.Substring(pluralString.Length - 2), "ch",
                        StringComparison.InvariantCultureIgnoreCase)) {
        pluralString += "e";
      }
      switch (lastCharacter) {
        case "s":
          return pluralString + "es";
        default:
          return pluralString + "s";
      }
    }
  }

}
